package com.sin.j2me.framework;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import java.util.Vector;

import javax.microedition.io.HttpConnection;
import com.sin.j2me.common.ExceptionHandle;
import com.sin.j2me.http.HttpRequest;

public class SinUtil {	
	public static boolean CloseHttpConnection(HttpConnection con){
		try {
			con.close();
			return true;
		} catch (IOException e) {
			NewDebug.e(e.getMessage());
			e.printStackTrace();
		}
		return false;
	}
	
	
	public static String HttpGet(String url){
		String res = null;
		try {
			HttpRequest request = new HttpRequest();
			InputStream is = request.getHttpPostInputStream(url, null);
			InputStreamReader isr = new InputStreamReader(is, "utf-8");
			int ch;
			StringBuffer sb = new StringBuffer();
			while ((ch = isr.read()) != -1) {
				sb.append((char)ch);
			}
			is.close();
			request.close();
			res = sb.toString();
		}
		catch (Exception e) {
			res = null;
			ExceptionHandle.handle(e);
		}
		return res;
	}
	
	private static final String URL_UNRESERVED = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789-_.~";
	private static final char[] HEX = "0123456789ABCDEF".toCharArray();  
	public static String URLEncode(String str)
	{
		StringBuffer buf = new StringBuffer();
		byte[] bytes = null;
		try
		{
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			DataOutputStream dos = new DataOutputStream(bos);
			dos.writeUTF(str);
			bytes = bos.toByteArray();
		}
		catch (IOException e)
		{
		}
		for (int i = 2; i < bytes.length; i++)
		{
			byte b = bytes[i];
			if (URL_UNRESERVED.indexOf(b) >= 0)
			{
				buf.append((char) b);
			}
			else
			{
				buf.append('%').append(HEX[(b >> 4) & 0x0f]).append(HEX[b & 0x0f]);
			}
		}
		return buf.toString();
	}
	
	public static Date getNow(){
		return new Date(System.currentTimeMillis());
	}
	
	public static String date2String(Date date){
		return date2String(date, TimeZone.getDefault());
	}
	
	public static String DATE_SPLIT = "-";
	public static String TIME_SPLIT = ":";
	public static String DATETIME_SPLIT = " ";
	public static String date2String(Date date, TimeZone zone){
		StringBuffer sb = new StringBuffer();
		
		Calendar calendar = Calendar.getInstance(zone);
		calendar.setTime(date);
		sb.append(calendar.get(Calendar.YEAR));
		sb.append(DATE_SPLIT);
		
		if((calendar.get(Calendar.MONTH)+1)<10)
			sb.append('0');
		sb.append(calendar.get(Calendar.MONTH)+1);
		sb.append(DATE_SPLIT);
		
		if(calendar.get(Calendar.DAY_OF_MONTH)<10)
			sb.append('0');
		sb.append(calendar.get(Calendar.DAY_OF_MONTH));
		sb.append(DATETIME_SPLIT);
		
		if(calendar.get(Calendar.HOUR_OF_DAY)<10)
			sb.append('0');
		sb.append(calendar.get(Calendar.HOUR_OF_DAY));
		sb.append(TIME_SPLIT);
		
		if(calendar.get(Calendar.MINUTE)<10)
			sb.append('0');
		sb.append(calendar.get(Calendar.MINUTE));
		sb.append(TIME_SPLIT);
		
		if(calendar.get(Calendar.SECOND)<10)
			sb.append('0');
		sb.append(calendar.get(Calendar.SECOND));
		return sb.toString();
	}
	
	public static Date string2Date(String str){
		return string2Date(str, TimeZone.getDefault());
	}
	
	public static Date string2Date(String str, TimeZone zone){
		String[] s = stringSplit(str, DATETIME_SPLIT);
		String[] date = stringSplit(s[0], DATE_SPLIT);
		String[] time = stringSplit(s[1], TIME_SPLIT);

		int year = Integer.parseInt(date[0]);
		int month = Integer.parseInt(date[1])-1;
		int day = Integer.parseInt(date[2]);
		int hour = Integer.parseInt(time[0]);
		int minute = Integer.parseInt(time[1]);
		int second = Integer.parseInt(time[2]);
		Calendar calendar = Calendar.getInstance(zone);
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, month);
		calendar.set(Calendar.DAY_OF_MONTH, day);
		calendar.set(Calendar.HOUR_OF_DAY, hour);
		calendar.set(Calendar.MINUTE, minute);
		calendar.set(Calendar.SECOND, second);
		
		return calendar.getTime();
	}
	
	
	public static String[] stringSplit(String str, String spt){
		Vector split = new Vector();
		int sptlen = spt.length();
		for(int i=0; i<str.length(); ){
			int ix = str.indexOf(spt, i);
			String itm = null;
			if(ix<0){
				itm = str.substring(i);
				split.addElement(itm);
				break;
			}
			else{
				itm = str.substring(i, ix);
				split.addElement(itm);
				i = (ix+sptlen);
			}
		}
		String[] ay = new String[split.size()];
		split.copyInto(ay);
		return ay;
	}
	
	
}
